home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 4_7.lha / 4_7 / 4_7b.c < prev    next >
Text File  |  1993-08-08  |  921b  |  42 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. include <tnode.h>
  6. include <string.h>
  7. include <error.h>
  8.  
  9. / insert a new node to the left, right
  10. / or at the node pointed to by the treeheadptr
  11. oid addnode(tree treeheadptr, char *nword)
  12.  
  13.    // a leaf found, enter the node here
  14.    if (!*treeheadptr)
  15. {
  16. if (strlen(nword) >= 20)
  17.     error("word too long in addnode");
  18.  
  19. tnode *n = new tnode;
  20. *treeheadptr = n;
  21. n->left = n->right = 0;
  22. n->count = 1;
  23. strcpy(n->tword, nword);
  24. return;
  25. }
  26.  
  27.    // check the name
  28.    int cmp = strcmp(nword, (*treeheadptr)->tword);
  29.  
  30.    // enter in the left subtree
  31.    if (cmp < 0)
  32. addnode(&(*treeheadptr)->left, nword);
  33.  
  34.    // enter in right subtree
  35.    else if (cmp > 0)
  36. addnode(&(*treeheadptr)->right, nword);
  37.  
  38.    // equal, increment the reference count
  39.    else /* if (cmp == 0) */
  40. (*treeheadptr)->count++;
  41.  
  42.